package com.example.itshome.adapter;


import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.impl.ViewOutlineProviderImpl;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.CollectCommodityRequest;
import com.example.itshome.view.CommodityDialog;

import java.util.List;

public class ShopPageRecycleViewAdapter extends RecyclerView.Adapter<ShopPageRecycleViewAdapter.ShopPageViewHolder> {

    private final List<CommodityEntity> mData;
    private final Context context;

    public ShopPageRecycleViewAdapter(Context context,List<CommodityEntity> list){
        mData=list;
        this.context=context;
    }
    @NonNull
    @Override
    public ShopPageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= View.inflate(context, R.layout.recycle_shop_page_item,null);
        view.setOutlineProvider(new ViewOutlineProviderImpl(20));
        view.setClipToOutline(true);
        ShopPageViewHolder holder=new ShopPageViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShopPageViewHolder holder, int position) {
        CommodityEntity entity=mData.get(position);
        holder.nameTextView.setText(entity.title);
        holder.priceTextView.setText(entity.price);
        holder.watchCountTextView.setText(entity.watchCount);
        holder.collectCountTextView.setText(entity.favoriteCount);
        holder.collectButton.setText(!entity.isCollection?"收藏":"取消收藏");

        holder.collectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new CollectCommodityRequest((Activity) context)
                        .setCollectButton(holder.collectButton)
                        .setIsCollect(entity)
                        .setCommodityId(entity.id);
                request.start();
            }
        });

        Glide.with(context).load(entity.surfacePictureSrc).placeholder(R.drawable.sample_picture).into(holder.imageView);

        holder.wholeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new CommodityDialog(context,entity).setOutSideCollectButton(holder.collectButton).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ShopPageViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        Button collectButton;
        TextView nameTextView;
        TextView priceTextView;
        TextView collectCountTextView;
        TextView watchCountTextView;
        View wholeLayout;
        public ShopPageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imv_recycle_shop_image);
            collectButton=itemView.findViewById(R.id.btn_recycle_shop_favorite);
            nameTextView=itemView.findViewById(R.id.txv_recycle_shop_commodity_name);
            priceTextView=itemView.findViewById(R.id.txv_recycle_shop_price);
            collectCountTextView=itemView.findViewById(R.id.txv_recycle_shop_favorite_count);
            watchCountTextView=itemView.findViewById(R.id.txv_recycle_shop_watch_count);
            wholeLayout=itemView.findViewById(R.id.layout_shop_page_whole);

            imageView.setOutlineProvider(new ViewOutlineProviderImpl(20));
            imageView.setClipToOutline(true);
        }
    }
}
